package com.example.demo.service;

import com.example.demo.mapper.BuyerOrderMapper;
import com.example.demo.mapper.CommentMapper;
import com.example.demo.model.CommentInfo;
import com.example.demo.model.OrderInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class BuyerOrderService  {
    
    private static final Logger log = LoggerFactory.getLogger(BuyerOrderService.class);
    
    @Autowired
    private BuyerOrderMapper buyerOrderMapper;
    
    @Autowired
    private CommentMapper commentMapper;
    
    @Autowired
    private CommentService commentService;
    

    public List<OrderInfo> getBoughtOrders(Integer buyerId, String status) {
        return buyerOrderMapper.getBoughtOrders(buyerId, status);
    }

    public List<OrderInfo> getBoughtOrdersComment(Integer buyerId, String status) {
        return buyerOrderMapper.getBoughtOrdersComment(buyerId, status);
    }

    public OrderInfo getOrderDetail(String orderNo, Integer buyerId) {
        return buyerOrderMapper.getOrderDetail(orderNo, buyerId);
    }
    

    @Transactional
    public int confirmReceive(String orderNo, Integer buyerId) {
        return buyerOrderMapper.confirmReceive(orderNo, buyerId);
    }
    

    @Transactional
    public int cancelOrder(String orderNo, Integer buyerId, String reason) {
        return buyerOrderMapper.cancelOrder(orderNo, buyerId, reason);
    }



    @Transactional
    public int deleteOrder(String orderNo, Integer buyerId, String reason) {
        // 如果是待付款状态，先取消订单
        OrderInfo order = getOrderDetail(orderNo, buyerId);
        if (order != null && "待付款".equals(order.getStatus())) {
            cancelOrder(orderNo, buyerId, reason);
        }
        return buyerOrderMapper.deleteOrder(orderNo, buyerId);
    }
    

    public String getCancelReason(String orderNo, Integer buyerId) {
        return buyerOrderMapper.getCancelReason(orderNo, buyerId);
    }
    

    @Transactional
    public int updateAddress(String orderNo, int buyerId, int addressId) {
        return buyerOrderMapper.updateAddress(orderNo, buyerId, addressId);
    }
    

    public Map<String, Object> getExpressInfo(String orderNo, Integer buyerId) {
        return buyerOrderMapper.getExpressInfo(orderNo, buyerId);
    }

    @Transactional
    public int confirmReceipt(String orderNo, Integer buyerId) {
        try {
            // 1. 验证订单是否属于该买家
            OrderInfo order = buyerOrderMapper.getOrderDetail(orderNo, buyerId);
            if (order == null) {
                log.warn("订单不存在或不属于该买家: orderNo={}, buyerId={}", orderNo, buyerId);
                return 0;
            }

            // 2. 验证订单状态是否为待收货
            if (!"待收货".equals(order.getStatus())) {
                log.warn("订单状态不正确: status={}", order.getStatus());
                return 0;
            }

            // 3. 创建初始评论记录
            boolean commentCreated = commentService.createInitialComment(
                orderNo, buyerId, order.getProductId(), order.getSellerId());
            if (!commentCreated) {
                log.error("创建初始评论记录失败: orderNo={}", orderNo);
                throw new RuntimeException("创建评论记录失败");
            }

            // 4. 更新订单状态
            return buyerOrderMapper.confirmReceive(orderNo, buyerId);
        } catch (Exception e) {
            log.error("确认收货失败", e);
            throw e;
        }
    }
}